package com.superjson.showpro.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.superjson.showpro.base.BaseObj;
import com.superjson.showpro.shared.enums.FileTypeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/**
 * Created by Jonathan on 2017/2/9.
 * 附件表
 */
@Entity
@Table(name = "sp_attach")
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Attach extends BaseObj implements Serializable {

    private static final long serialVersionUID = -4306097393860578493L;

    //所在文件夹
    @Column(name = "relative_folder")
    private String relativeFolder;

    //原始名
    @Column(name = "original_name", length = 125)
    private String originalName;

    //现文件名
    @Column(length = 125)
    private String name;

    // 后缀
    @Column(name = "suffix_name", length = 125)
    private String suffixName;

    // 前缀
    @Column(name = "prefix_name", length = 125)
    private String prefixName;

    // 附件大小
    private Long size;

    // 描述
    @Column(name = "description", length = 255)
    private String description;

    //文件类型
    @Column(name = "type")
    @Enumerated(EnumType.STRING)
    private FileTypeEnum type;

    //相对路径
    @Column(name = "relative_path")
    private String relativePath;

    //上传时间
    @JSONField(name = "upLoad_time", format = "yyyyMMdd")
    @Column(name = "upLoad_time")
    private Date upLoadTime;


    @Transient
    public String getLocaleUrl() {
        return relativeFolder.endsWith("/") ? relativeFolder + name : relativeFolder + "/" + name;
    }

    @Transient
    public String getSizeFormat() {
        String sizeStr;
        if (size < 1024) sizeStr = size + "B";
        else if (size < 1024 * 1024) sizeStr = size / 1024 + "KB";
        else if (size < 1024 * 1024 * 1024) sizeStr = size / 1024 / 1024 + "MB";
        else if (size < 1024 * 1024 * 1024 * 1024) sizeStr = size / 1024 / 1024 / 1024 + "GB";
        else sizeStr = "未知大小";
        return sizeStr;
    }

}
